package com.github.sd4324530.fastweixin.api;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sd4324530.fastweixin.api.config.WxConfig;
import com.github.sd4324530.fastweixin.api.enums.ResultType;
import com.github.sd4324530.fastweixin.api.response.BaseResponse;
import com.github.sd4324530.fastweixin.message.BaseMsg;
import com.github.sd4324530.fastweixin.message.ImageMsg;
import com.github.sd4324530.fastweixin.message.MpNewsMsg;
import com.github.sd4324530.fastweixin.message.TextMsg;
import com.github.sd4324530.fastweixin.message.VideoMsg;
import com.github.sd4324530.fastweixin.message.VoiceMsg;
import com.github.sd4324530.fastweixin.message.WxCardMsg;
import com.github.sd4324530.fastweixin.util.BeanUtil;
import com.github.sd4324530.fastweixin.util.JSONUtil;

/**
 * 预览接口API
 * 通过该接口发送消息给指定用户，在手机端查看消息的样式和排版。
 * 为了满足第三方平台开发者的需求，在保留对openID预览能力的同时，增加了对指定微信号发送预览的能力
 * 但该能力每日调用次数有限制（100次），请勿滥用
 * @author TylerCheung
 * @since 1.1
 */
public class MessagePreviewAPI extends BaseAPI {

    private static final Logger LOG = LoggerFactory.getLogger(MessagePreviewAPI.class);

    public MessagePreviewAPI(WxConfig config) {
        super(config);
    }

    /**
     * 预览接口发送预览信息给用户
     *
     * @param openid  关注者ID
     * @param message 消息对象，支持各种消息类型
     * @return 调用结果
     * @deprecated 
     */
    @Deprecated
    public ResultType sendPreview(String openid, BaseMsg message,String msgtype) {
        BeanUtil.requireNonNull(openid, "openid is null");
        BeanUtil.requireNonNull(msgtype, "msgtype is null");
        LOG.debug("发布客服消息......");
        String url = BASE_API_URL + "cgi-bin/message/mass/preview?access_token=#";
        final Map<String, Object> params = new HashMap<String, Object>();
        params.put("touser", openid);
        if (message instanceof TextMsg) {
            TextMsg msg = (TextMsg) message;
            params.put("msgtype", "text");
            Map<String, String> text = new HashMap<String, String>();
            text.put("content", msg.getContent());
            params.put("text", text);
        } else if (message instanceof ImageMsg) {
            ImageMsg msg = (ImageMsg) message;
            params.put("msgtype", "image");
            Map<String, String> image = new HashMap<String, String>();
            image.put("media_id", msg.getMediaId());
            params.put("image", image);
        } else if (message instanceof VoiceMsg) {
            VoiceMsg msg = (VoiceMsg) message;
            params.put("msgtype", "voice");
            Map<String, String> voice = new HashMap<String, String>();
            voice.put("media_id", msg.getMediaId());
            params.put("voice", voice);
        } else if (message instanceof VideoMsg) {
            VideoMsg msg = (VideoMsg) message;
            params.put("msgtype", "video");
            Map<String, String> video = new HashMap<String, String>();
            video.put("media_id", msg.getMediaId());
            video.put("thumb_media_id", msg.getMediaId());
            video.put("title", msg.getTitle());
            video.put("description", msg.getDescription());
            params.put("video", video);
        } else if (message instanceof WxCardMsg) {
        	WxCardMsg msg = (WxCardMsg) message;
            params.put("msgtype", "wxcard");
            Map<String, String> wxcard = new HashMap<String, String>();
            wxcard.put("card_id", msg.getCard_id());
            wxcard.put("card_ext", "");
            params.put("wxcard", wxcard);
        } else if (message instanceof MpNewsMsg) {
        	MpNewsMsg msg = (MpNewsMsg) message;
            params.put("msgtype", "mpnews");
            Map<String, String> mpnews = new HashMap<String, String>();
            mpnews.put("media_id", msg.getMediaId());
            params.put("mpnews", mpnews);
        }
        BaseResponse response = executePost(url, JSONUtil.toJson(params));
        return ResultType.get(response.getErrcode());
    }

}
